<?php
/**
 * Created by PhpStorm.
 * User: wls
 * Date: 2018/11/13
 * Time: 21:16
 */

namespace app\admin\model;

use think\Db;
class FamilyActivity extends DataBase
{
    protected $name = 'family_activity';
   protected $autoWriteTimestamp = true;

    /**
     * @return \think\Paginator
     * @throws \think\exception\DbException
     * @user shuaishuaiwu
     * @date 2018/11/13
     * @time 23:05
     * @title 预审家族数据
     */

//    public function getStatusAttr($value)
//    {
//        $status = [1=>'审核中',2=>'复审',3=>'已通过',4=>'已解散'];
//        return $status[$value];
//    }
    public function getDataList()
    {
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        );
        $where = true;
        $key = input('key'); $value = input('value');

        if($key=='nickname' && !empty($value)){

            $userId = User::where($key,'=',$value)->value('id');
            $where[] = ['b.user_id','=',$userId];

        }elseif($key=='f_name' && !empty($value)){

            $where[] = ['b.f_name','=',$value];

        }elseif($key=='u_id' && !empty($value)){

            $userId = User::where($key,'=',$value)->value('id');
            $where[] = ['b.user_id','=',$userId];
        }
        $data = $this
            ->alias('a')
            ->field('a.id,a.room_id,a.family_id,a.create_time,a.recheck_time,a.status,b.user_id,b.f_name,b.f_liveness,c.r_id,c.r_name')
            ->join('family b','a.family_id = b.id','left')
            ->join('rooms c','a.room_id = c.id','left')
            ->where($where)
            ->order('a.id desc')
            ->paginate($page_config);

        foreach($data as $key => $value){
            $userInfo =  User::field('u_id,nickname')->where('id','=',$value['user_id'])->find();
            $data[$key]['u_id'] =$userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'] ;
            $data[$key]['num'] = FamilyMembers::where('family_id','=',$value['family_id'])->count();
            $map2[] = ['create_time','between',[$value['create_time'],intval($value['create_time'])+15*24*3600]];
            $map2[] = ['family_id','=',$value['family_id']];
            $data[$key]['check_num'] = familyMembers::where($map2)->count();
            if($value['recheck_time'] > (intval($value['create_time']) + 7 * 24 * 3600)){
                $map3[] = ['create_time','between',[$value['recheck_time'],intval($value['recheck_time'])+15*24*3600]];
                $map3[] = ['family_id','=',$value['family_id']];
                $data[$key]['recheck_num'] = familyMembers::where($map2)->count();
            }else{
                $data[$key]['recheck_num'] = 'N/A';
            }
            //判断是否需要复审 1:复审  2：不需要复审
            $data[$key]['recheck_status'] = (time() >= (strtotime($data[$key]['create_time']) + 7 * 24 * 3600)) ? 1 : 2;

        }
       // dump($data);
        return $data;
    }


    //同意预审家族
    public function checkFamilyAgree()
    {
        $id = input('get.id');
        Db::connect('db_data')->startTrans();
        try{
            $family_id = $this->where('id','eq',$id)->value('family_id');
            $this->where('id','eq',$id)->update(['status'=>3]);
            $familyInfo = Family::where('id','eq',$family_id)->find();
            Family::where('id','eq',$family_id)->update(['type'=>1]);
            $arr['user_id'] =$familyInfo['user_id'];
            $arr['title'] = '你的预审家族已通过';
            $arr['content'] = '你的预审家族于'.date('Y-m-d H:i:s').'成功通过预审家族审批，如有疑问请联系绯闻客服';
            $arr['have_read'] = 1;
            $arr['type'] = 2;
            $arr['status'] = 1;
            $arr['create_time'] = time();
            Message::create($arr);
            Db::connect('db_data')->commit();
            return ['code'=>100, 'msg'=>'成功'];
        }catch(\Exception $e){
            Db::connect('db_data')->rollback();
            return ['code'=>-100,'msg'=>'失败:'.$e->getMessage()];
        }
    }
    //复审
    public function checkFamilyReview()
    {
        $id = input('get.id');
        $familyActivityInfo = $this->where('id','eq',$id)->find();
        if( intval($familyActivityInfo['create_time']) + 24*7*3600 > time())
            return ['code'=>-100,'msg'=>'7天内不能复审'];

        Db::connect('db_data')->startTrans();
        try{
            $this->where('id','eq',$id)->update(['status'=>2,'recheck_time'=>time()]);
            $familyInfo = Family::where('id','eq',$familyActivityInfo['family_id'])->find();
            Family::where('id','eq',$familyActivityInfo['family_id'])->update(['type'=>3]);
            $arr['user_id'] =$familyInfo['user_id'];
            $arr['title'] = '你的预审家族进行复审';
            $arr['content'] = '你的预审家族于'.date('Y-m-d H:i:s').'进行预审家族复审中，如有疑问请联系绯闻客服';
            $arr['have_read'] = 1;
            $arr['type'] = 2;
            $arr['status'] = 1;
            $arr['create_time'] = time();
            Message::create($arr);
            Db::connect('db_data')->commit();
            return ['code'=>100, 'msg'=>'成功'];
        }catch(\Exception $e){
            Db::connect('db_data')->rollback();
            return ['code'=>-100,'msg'=>'失败:'.$e->getMessage()];
        }

    }
    //解散
    public function checkFamilyDismiss()
    {
        $id = input('get.id');

        Db::connect('db_data')->startTrans();
        try{
            $familyActivityInfo = $this->where('id','eq',$id)->find();
            $this->where('id','eq',$id)->update(['status'=>4]);
            $familyInfo = Family::where('id','eq',$familyActivityInfo['family_id'])->find();
            Family::where('id','eq',$familyActivityInfo['family_id'])->update(['type'=>4,'status'=>2]);

            $userIds = FamilyMembers::where([ ['family_id','eq',$familyActivityInfo['family_id'] ],['check','eq',2] ])->column('user_id');
            if(!empty($userIds)){

                foreach( $userIds as $k => $v ){

                    $arr['user_id'] =$v;
                    $arr['title'] = '您加入的家族已解散';
                    $arr['content'] = '您的账号于'.date('Y-m-d H:i:s').'退出了家族，如有疑问请联系绯闻客服';
                    $arr['have_read'] = 1;
                    $arr['type'] = 2;
                    $arr['status'] = 1;
                    $arr['create_time'] = time();
                    Message::create($arr);
                }
            }
            $info = [
                'user_id' =>  $familyInfo['user_id'],
                'title' => "您的预审家族已解散",
                'content' => "您的账号于" . date("Y-m-d H:i:s") . "解散了预审家族，如有疑问请联系绯闻客服！",
                'have_read' => 1,
                'type' => 2,
                'status' => 1,
                'create_time' => time(),
            ];
            Message::create($info);
            Db::connect('db_data')->commit();
            return ['code'=>100, 'msg'=>'解散成功'];
        }catch(\Exception $e){
            Db::connect('db_data')->rollback();
            return ['code'=>-100,'msg'=>'解散失败:'.$e->getMessage()];
        }
    }

}